use "${datadir}\HTB_main_cleaned_counterfactual", replace

keep if subsample & inrange(bkhp_mortgage, -5, 20)
keep if dealtype==2 & ratetype==1
keep if LTV>60

gen LTV_integer = 5 * ceil(LTV/5)

gen post = year>2015

egen jl_kt_id 	= group(repayment ratetype dealtype lender_id advtype yearm_date) 	

gen myLTV = LTV_integer
gen const = 0
gen pred_ltv = 0

forval i = 0/1 {
	xi i.myLTV
	reghdfe  interest _ImyLTV_* if post==`i', abs(jl_kt_id, savefe) resid
	predict tmp_pred_ltv, xb
	replace pred_ltv = tmp_pred_ltv if post==`i'
	foreach x of varlist _ImyLTV_* {
	 replace `x' = 0
	}
	predict tmp_pred, xbd
	sum tmp_pred, meanonly
	replace const = `r(mean)' if post==`i'
	drop tmp_pred_ltv tmp_pred 
}


gen cond_interest = const + pred_ltv

collapse (count) tot = loan_val (median) bkhp_mortgage interest cond_interest, by(LTV_integer post)

bys post: egen den = sum(tot)
gen share = tot/den

twoway ///
	(connected cond_interest LTV_integer if post == 0, msize(med) sort) ///
	(connected cond_interest LTV_integer if post == 1, msize(med) sort msymbol(D)) ///
	(scatter cond_interest LTV_integer [aweight = share] if post == 0, color(navy)) ///
	(scatter cond_interest LTV_integer [aweight = share] if post == 1, msymbol(D) color(maroon)), ///
	xline(80, lwidth(vthin) lcolor(black) lpattern(dash)) ///	
	ytitle("Median interest (%)") ///
	xtitle("Loan-to-value") ///
	legend(ring(0) position(11) order( 1 "2013-2015" 2 "2016-2017") ) ///
	graphregion(color(white)) bgcolor(white) name(bkhp1, replace) 

twoway ///
	(connected bkhp_mortgage LTV_integer if post == 0, msize(med) sort) ///
	(connected bkhp_mortgage LTV_integer if post == 1, msize(med) sort msymbol(D)) ///
	(scatter bkhp_mortgage LTV_integer [aweight = share] if post == 0, color(navy)) ///
	(scatter bkhp_mortgage LTV_integer [aweight = share] if post == 1, msymbol(D) color(maroon)), ///
	xline(80, lwidth(vthin) lcolor(black) lpattern(dash)) ///	
	ytitle("Break-even house price appreciation (%)") ///
	xtitle("Loan-to-value") ///
	legend(ring(0) position(11) order( 1 "2013-2015" 2 "2016-2017") ) ///
	graphregion(color(white)) bgcolor(white) name(bkhp2, replace)


gr combine bkhp1 bkhp2, graphregion(color(white)) xsize(8)
graph export "${figuredir}\distribution_interest_bkhp.pdf", ///
	as(pdf) replace	



